package com.choupin.choupin.admin.controller;

import com.choupin.choupin.admin.entity.UpdateResult;
import com.choupin.choupin.admin.entity.User;
import com.choupin.choupin.admin.mapper.ManageUserMapper;
import com.choupin.choupin.admin.service.IManageUser;
import com.choupin.choupin.controller.BaseController;
import com.choupin.choupin.entity.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/adminuser")
public class ManageUserController extends BaseController {

    @Autowired
    IManageUser manageUser;

    @CrossOrigin
    @PostMapping("/show")
    public JsonResult<List<User>> showUser(@RequestBody Map<String,Object> data){
        JsonResult<List<User>> jsonResult=new JsonResult<>();
        List<User> userList=manageUser.showUser((Integer) data.get("currentPage"),(Integer) data.get("pageSize"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(userList);
        return  jsonResult;
    }

//    联合搜索
    @CrossOrigin
    @PostMapping("/select")
    public JsonResult<List<User>> selectUsers(@RequestBody Map<String,Object> data ){
        JsonResult<List<User>> jsonResult=new JsonResult<>();
        List<User> userList=manageUser.selectUsers((String) data.get("id"),(String) data.get("phone"),(String) data.get("nickname"),(Integer) data.get("currentPage"),(Integer) data.get("pageSize"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(userList);
        return jsonResult;
    }

//    显示更新信息
     @CrossOrigin
    @PostMapping("/showupdate")
    public JsonResult<UpdateResult> showUpdate(@RequestBody Map<String,Object> data){
        JsonResult<UpdateResult> jsonResult=new JsonResult<>();
        UpdateResult updateResult=manageUser.showUpdate((String) data.get("phone"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(updateResult);
        return jsonResult;
     }

//     修改用户数据
    @CrossOrigin
    @PostMapping("/update")
    public JsonResult<UpdateResult> update(@RequestBody Map<String,Object> data){
        JsonResult<UpdateResult> jsonResult=new JsonResult<>();
        UpdateResult params=new UpdateResult();
        params.setPhone((String) data.get("phone"));
        params.setIsDelete((Integer.parseInt((String) data.get("isDelete"))));
        String endTimeStr= (String) data.get("endTime");
        System.out.println("取出时间："+endTimeStr);
        //        时间转换
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(endTimeStr!=null){
            LocalDateTime  endTime=LocalDateTime.parse(endTimeStr.replace("T"," "),formatter);
            params.setEndTime(endTime);
        }
        params.setWeight(Integer.parseInt((String) data.get("weight")));
        params.setMemberStatus(Integer.parseInt((String) data.get("memberStatus")));
        System.out.println("修改用户数据！");
        UpdateResult updateResult=manageUser.update(params);
        jsonResult.setState(SUCCESS);
        jsonResult.setData(updateResult);
        return  jsonResult;
    }


}
